import random
import time

from PyGameAutoAndroid import *


def find_lv55_cat():
    ret = xp.findColor("#2930DB", "1|1|#4E4F50, -1|-1|#091137", 142, 81, 146, 85)
    if ret:
        # xp.console("找到猫")
        return True
    else:
        # xp.console("不是猫")
        return False


def find_lv50_cat():
    ret = xp.findColor("#EEDFAF", "1|1|#6E5E33, -1|-1|#EEDFAF", 143, 72, 147, 76)
    if ret:
        return True
    else:
        return False


def find_battle_wolf():
    ret = xp.findColor("#FF7272", "-1|0|#6E9E9E", 119, 32, 459, 101)
    if ret:
        return True
    else:
        return False


def check_minimap():
    ret = xp.findColor("#E2D57E", "1|0|#E2D57E", 1078, 137, 1080, 138)
    if ret:
        return True
    else:
        return False


def is_map_opened():
    ret = xp.matchColor("#E9E9E9", 1236, 20)
    if ret:
        return True
    else:
        return False


def is_town_menu_opened():
    ret = xp.matchColor("#255971", 982, 643)
    if ret:
        return True
    else:
        return False


def open_minimap():
    while True:
        if check_minimap():
            break
        else:
            time.sleep(0.2)
    while True:
        if is_map_opened():
            break
        else:
            xp.tap(1078, 137)
            time.sleep(0.2)


def open_worldmap():
    while True:
        if check_minimap():
            break
        else:
            time.sleep(0.2)
    while True:
        if is_map_opened():
            break
        else:
            xp.tap(469, 624)
            time.sleep(0.5)


def heal(x, y):
    while True:
        if check_minimap():
            break
        else:
            time.sleep(0.2)
    xp.tap(x, y)
    time.sleep(0.5)


def goto_wild():
    while True:
        if check_minimap():
            break
        else:
            time.sleep(0.2)
    find_color_click("#FFFFFF", 120, 138)
    find_color_click("#000000", 108, 333)


def goto_town(tx, ty):
    while True:
        if is_town_menu_opened():
            break
        else:
            xp.tap(tx, ty)
            time.sleep(1.2)
    find_color_click("#2D637B", 1053, 657)
    find_color_click("#2D6279", 797, 496)


def wait(t):
    delta = t/10
    time.sleep(random.uniform(t, t+delta))


def find_color_click(color, x, y, cx=None, cy=None):
    if cx is None:
        cx = x
    if cy is None:
        cy = y
    while True:
        ret = xp.matchColor(color, x, y)
        if not ret:
            wait(0.1)
            continue
        wait(0.3)
        xp.tap(cx, cy, 0.1)
        break


def find_no_color_click(color, x, y, cx=None, cy=None):
    if cx is None:
        cx = x
    if cy is None:
        cy = y
    while True:
        ret = xp.matchColor(color, x, y)
        if ret:
            wait(0.1)
            continue
        wait(0.3)
        xp.tap(cx, cy)
        break


def goto_town_old():
    # xp.console("尝试进城")
    find_color_click("#BBB652", 640, 364, cx=821, cy=198)
    find_color_click("#2D637B", 1053, 657)
    find_color_click("#2D6279", 797, 496)


def heal_self():
    # xp.console("尝试回血")
    find_color_click("#FFFFFF", 643, 203)
    find_color_click("#000000", 643, 203)
    # xp.console("1")
    heal(698, 262)
    # xp.console("2")
    find_color_click("#D0C4B4", 441, 144)
    while True:
        ret = xp.matchColor("#E2D57E", 1081, 137)
        if ret:
            wait(1)
            xp.tap(471, 611)
            break
        else:
            wait(0.1)
            xp.tap(796, 506)
            continue
    # xp.console("尝试去野外")
    find_color_click("#E9E9E9", 1236, 21, cx=834, cy=422)
    find_color_click("#2D637B", 1052, 659)
    find_color_click("#2C627B", 797, 498)


def moving():
    xp.swipe(1200, 700, 1000, 700)
    wait(0.5)
    xp.swipe(1000, 700, 1200, 700)
    wait(0.5)


def is_battle():
    ret = xp.matchColor("#FFFFFF", 1077, 627)
    if ret:
        return True
    else:
        return False


def cait_battle_sequence():
    while True:
        ret = xp.matchColor("#000000", 756, 357)
        if not ret:
            wait(2)
            # xp.tap(756, 357)
            continue
        break


def battle_sequence():
    find_color_click("#548654", 1074, 79)
    wait(0.3)
    find_color_click("#6F6F6F", 919, 564, cx=714, cy=516)
    wait(0.5)
    # if find_battle_wolf() is True:
    find_color_click("#548654", 1069, 221)
    wait(0.3)
    find_color_click("#6F6F6F", 919, 564, cx=714, cy=516)
    wait(0.5)
    find_color_click("#FFFFFF", 1083, 643, cx=864, cy=643)
    find_color_click("#FFFFFF", 1083, 643)

    while True:
        ret = xp.matchColor("#000000", 756, 357)
        if not ret:
            wait(0.1)
            xp.tap(756, 357)
            continue
        break


while True:
    count = 0
    req_init = True
    while True:
        if count == 0 and req_init:
            open_minimap()
            xp.tap(765, 523)
            wait(10)
            req_init = False
        if count == 8:
            break
        moving()
        if is_battle() is True:
            time.sleep(0.5)
            if find_lv55_cat() is True:
                cait_battle_sequence()
                break
            elif find_lv50_cat() is True:
                xp.tap(540, 643)
                wait(1)
            else:
                battle_sequence()
                count += 1
        else:
            continue
    open_worldmap()
    goto_town(538, 321)
    heal_self()
    time.sleep(1)
    goto_wild()
